package toy.keli.edic.ui.home.components

import android.widget.Toast
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.dp
import toy.keli.edic.R
import toy.keli.edic.ui.home.dictionaryStringBuilder
import toy.keli.edic.ui.main.components.Display.mt
import toy.keli.edic.print.MPrinter
import toy.keli.edic.ui.main.extend.MText as Text

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CopyButton(copydata:String = "",modifier: Modifier = Modifier) {
    val clipboardManager = LocalClipboardManager.current
    val context = LocalContext.current
    Card(
//        elevation = 0.dp,
//        onClick = {
//              onButtonClick()
//        },
        shape = RoundedCornerShape(8.dp),
        modifier =  Modifier.combinedClickable(
            onLongClick = {
                if (!dictionaryStringBuilder.toString().isNotEmpty()) return@combinedClickable
                if (MPrinter.printer.printView()) {
                    return@combinedClickable
                }
                clipboardManager.setText(buildAnnotatedString {
                    append(dictionaryStringBuilder.toString())
                })
                Toast.makeText(context, mt("Copied"), Toast.LENGTH_LONG).show()
//                coroutineScope.launch {
//                    scaffold.snackbarHostState.showSnackbar(message = "Copied")
//                }
            },
            onClick = {
                clipboardManager.setText(buildAnnotatedString {
                    append(copydata)
                })
                Toast.makeText(context, mt("Copied"), Toast.LENGTH_SHORT).show()
//                coroutineScope.launch {
//                    scaffold.snackbarHostState.showSnackbar(message = "Copied")
//
//                }
            }
        )
    ) {
        Column(
            verticalArrangement = Arrangement.SpaceAround,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = modifier
                .background(MaterialTheme.colorScheme.background)
                .padding(8.dp)
                .size(48.dp)
        ) {
            Icon(painterResource(id = R.drawable.copy), contentDescription = null,
                tint = MaterialTheme.colorScheme.onBackground)
            Text(
                text = "Copy",
                color = MaterialTheme.colorScheme.onBackground,
                style = MaterialTheme.typography.titleMedium
            )
        }
    }
}